<template>
  <el-card class="Role-container">
    <template #header>
      <div class="header">
        <h3 v-show="!type">选择所修课程</h3>
        <h3 v-show="type">本专业的课程</h3>
      </div>
    </template>
    <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%">
      <el-table-column
          prop="name"
          label="课程名"
          width="400"
      >
      </el-table-column>
      <el-table-column
          prop="teacherName"
          label="课程老师"
      >
      </el-table-column>
      <el-table-column
          prop="major"
          label="专业"
      >
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          width="100"
      >
        <template v-slot="scope">
          <el-switch v-model="scope.row.status"
                     active-color="#13ce66"
                     inactive-color="#ff4949"
                     disabled="true"
                     :active-value=1
                     :inactive-value=0>
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
      >
        <template #default="scope">
          <a v-if="scope.row.status===0" style="cursor: pointer; margin-right: 10px" @click="toTopicList(scope.row.id)">评价</a>
          <span v-if="scope.row.status!==0" style="cursor: pointer; margin-right: 10px">已评价</span>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="changePage"
    />
  </el-card>
</template>

<script>
import {onMounted, reactive, toRefs} from 'vue'
import {useRoute, useRouter} from 'vue-router'
import moment from "moment";
import axios from "@/utils/axios";
import {ElSwitch} from "element-plus";

export default {
  name: 'menu',
  components: {
    ElSwitch
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      type: undefined, // 操作类型
      planId: ""
    })
    onMounted(() => {
      state.planId = route.query.planId
      state.type = route.query.type
      getInfo()
    })
    // 获取分类列表
    const getInfo = () => {
      state.loading = true
      let url = "/evaluate/course/student";
      if(state.type === "teacher"){
        url = "/evaluate/course/teacher"
      }
      axios.get(url, {
        params: {
          current: state.currentPage,
          size: state.pageSize,
          planId: state.planId
        }
      }).then(res => {
        state.tableData = res.records
        state.total = res.total
        state.currentPage = res.current
        state.loading = false
      })
    }
    const changePage = (val) => {
      state.currentPage = val
      getInfo()
    }
    const toTopicList = (id) => {
      router.push({
        path: "/plan/topic", query: {
          courseId: id,
          planId: state.planId,
          type: state.type
        }
      })
    }
    return {
      ...toRefs(state),
      changePage,
      toTopicList,
      dateFormat: function (row, column) {
        const date = row[column.property];
        if (date === undefined) {
          return ''
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss")
      }
    }
  }
}
</script>

<style scoped>
.Role-container {
  min-height: 100%;
}

.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>